#include "tower.h"
#include <SDL/SDL_gfxPrimitives.h>
#include <SDL/SDL.h>
#include <iostream>
#include "game.h"

using namespace std;

extern SDL_Surface * screen;

CTower::CTower(int tx, int ty, CScene* scene) {
    SetX(tx);
    SetY(ty);
    SetOpDelay(1000);
    gamescene = scene;
    SetRadius(50);
    SetDMG(1);
    SetSpeedRatio(1);
    SetW(10);
    SetH(10);
    setTime(getTime() + GetOpDelay()*(getTicks()/GetOpDelay()));
    if (getTime() < 0) setTime(0);
}

void CTower::Do() {
    if (getTicks() >= GetOpDelay()) {
        for (int i = 0; i < getTicks()/GetOpDelay(); i++) {
            Shoot();
        }
        setTime(getTime() + GetOpDelay()*(getTicks()/GetOpDelay()));
    }
}

void CTower::Draw() {
    rectangleRGBA(screen,GetX()-GetW()/2,GetY()-GetH()/2,GetX()+GetW()/2,GetY()+GetH()/2,0xFF,0,0,0xFF);
    circleRGBA(screen,GetX(),GetY(),GetRadius(),0,0xFF,0,0xFF);
}

void CTower::Shoot() {
    for (auto it = gamescene->obj()->begin(); it != gamescene->obj()->end(); it++) {
        if ((*it)->isCreep() && ObjectDistance((**it),(*this)) <= GetRadius()*GetRadius()) {
            lineRGBA(screen,GetX(),GetY(),(*it)->GetX(),(*it)->GetY(),0,0,0xFF,0xFF);
            (*it)->Damage(GetDMG());
            break;
        }
    }
}

void CTower::SetGameScene(CScene *scenet) {
    gamescene = scenet;
}

CScene* CTower::ggamescene() {
    return gamescene;
}

CTower::CTower() {

}

CTower::~CTower() {

}

CYellowTower::CYellowTower(int tx, int ty, CScene *scene) {
    SetX(tx);
    SetY(ty);
    SetOpDelay(1000);
    SetGameScene(scene);
    SetRadius(50);
    SetDMG(1);
    SetSpeedRatio(1);
    SetW(10);
    SetH(10);
    setTime(getTime() + GetOpDelay()*(getTicks()/GetOpDelay()));
    if (getTime() < 0) setTime(0);
}

void CYellowTower::Shoot() {
    for (auto it = ggamescene()->obj()->begin(); it != ggamescene()->obj()->end(); it++) {
        if ((*it)->isCreep() && ObjectDistance((**it),(*this)) <= GetRadius()*GetRadius()) {
            lineRGBA(screen,GetX(),GetY(),(*it)->GetX(),(*it)->GetY(),0xFF,0xFF,0,0xFF);
            (*it)->Damage(GetDMG());
            break;
        }
    }
    for (auto it = ggamescene()->obj()->begin(); it != ggamescene()->obj()->end(); it++) {
        if ((*it)->isCreep() && ObjectDistance((**it),(*this)) <= GetRadius()*GetRadius()) {
            lineRGBA(screen,GetX(),GetY(),(*it)->GetX(),(*it)->GetY(),0xFF,0xFF,0,0xFF);
            (*it)->Damage(GetDMG());
            break;
        }
    }
    for (auto it = ggamescene()->obj()->begin(); it != ggamescene()->obj()->end(); it++) {
        if ((*it)->isCreep() && ObjectDistance((**it),(*this)) <= GetRadius()*GetRadius()) {
            lineRGBA(screen,GetX(),GetY(),(*it)->GetX(),(*it)->GetY(),0xFF,0xFF,0,0xFF);
            (*it)->Damage(GetDMG());
            break;
        }
    }
}
